package com.mxpio.erp.technology.service.impl;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.technology.entity.*;
import com.mxpio.erp.common.technology.entity.ProdTecRoutTmp;
import com.mxpio.erp.common.technology.service.ProdTecRoutTmpService;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class ProdTecRoutTmpServiceImpl extends BaseServiceImpl<ProdTecRoutTmp> implements ProdTecRoutTmpService {
    @Override
    @Transactional
    public ProdTecRoutTmp listRoutTmp(String productItemCode) {
        List<Item> itemList = JpaUtil.linq(Item.class).equal("itemCode",productItemCode).equal("itemSource","1").list();
        if(CollectionUtils.isEmpty(itemList)){
            return new ProdTecRoutTmp();
        }
        Item item = itemList.get(0);
        List<ProdTecRoutTmp> tmpList = JpaUtil.linq(ProdTecRoutTmp.class).equal("productItemCode",productItemCode).list();
        ProdTecRoutTmp routTmp;
        if(CollectionUtils.isEmpty(tmpList)){
            routTmp = new ProdTecRoutTmp();
            routTmp.setProductItemCode(item.getItemCode());
            routTmp.setProductItemName(item.getItemName());
            routTmp.setItemSpec(item.getItemSpec());
            routTmp.setProductDrawingNo(item.getDrawingNo());
        } else{
            routTmp = tmpList.get(0);
            List<ProdTecRoutProcTmp> lineTmpList = JpaUtil.linq(ProdTecRoutProcTmp.class)
                    .collect("processCode", ProcessInfo.class,"processCode")
                    .equal("routId",routTmp.getRoutId()).list();
            if (lineTmpList.size()>0){
                lineTmpList.forEach(per->per.setSaveTransient(false));
            }
            routTmp.setLineList(lineTmpList);
        }
        return routTmp;
    }

    @Override
    @Transactional(readOnly = true)
    public boolean validBeginTime(ProdTecRoutTmp routTmp) {
        List<ProdTecRout> bomInfos = JpaUtil.linq(ProdTecRout.class).equal("productItemCode",routTmp.getProductItemCode()).equal("primaryRout","1").list();
        if(CollectionUtils.isEmpty(bomInfos)){
            return true;
        }
        for(ProdTecRout rout:bomInfos){
            if(rout.getBeginTime().compareTo(routTmp.getBeginTime())>=0){
                return false;
            }
        }
        return true;
    }

    @Override
    @Transactional(readOnly = false)
    public boolean audit(String routId) {
        try{
            ProdTecRoutTmp bomInfoTmp = getById(ProdTecRoutTmp.class,routId);
            List<ProdTecRoutProcTmp> tmpList = JpaUtil.linq(ProdTecRoutProcTmp.class).equal("routId",routId).list();
            ProdTecRoutAudit prodTecRoutAudit = new ProdTecRoutAudit();
            BeanReflectionUtils.copyProperties(prodTecRoutAudit,bomInfoTmp);
            prodTecRoutAudit.setChangeCode(null);//清空变更单单号
            prodTecRoutAudit.setUseType(OrderStatusEnums.BomStatus.AUDITING.getStatusCode());
            JpaUtil.save(prodTecRoutAudit);
            for(ProdTecRoutProcTmp o:tmpList){
                ProdTecRoutProcAudit audit = new ProdTecRoutProcAudit();
                BeanReflectionUtils.copyProperties(audit,o);
                JpaUtil.save(audit);
            }
            bomInfoTmp.setUseType(OrderStatusEnums.BomStatus.USING.getStatusCode());
            JpaUtil.save(bomInfoTmp);
            return true;
        }
        catch(Exception e){
            log.error(e.getMessage(),e);
        }
        return false;
    }
}
